package kr.hkit.androidcomplete02.fragment;

import kr.hkit.androidcomplete02.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;

public class QuestionDialogFragment extends DialogFragment {
	NoticeDialogListener mListener;
	Bundle bundle;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			mListener = (NoticeDialogListener) activity;
		} catch (ClassCastException e) {
			String msg = " must implement NoticeDialogListener";
			throw new ClassCastException(activity.toString() + msg);
		}
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		bundle = getArguments();
		return new AlertDialog.Builder(getActivity())
				.setIcon(R.drawable.ic_launcher)
				.setTitle(bundle.getString("title"))
				.setMessage(bundle.getString("msg"))
				.setPositiveButton(bundle.getString("Q1"), listener)
				.setNegativeButton(bundle.getString("Q2"), listener).create();
	}

	OnClickListener listener = new OnClickListener() {
		public void onClick(DialogInterface dialog, int which) {
			int a = bundle.getInt("a");
			int b = bundle.getInt("b");
			switch (which) {
			case DialogInterface.BUTTON_POSITIVE:
				mListener.onDialogClick(QuestionDialogFragment.this, a + b);
				break;
			case DialogInterface.BUTTON_NEGATIVE:
				mListener.onDialogClick(QuestionDialogFragment.this, a * b);
				break;
			}
		}
	};

	public interface NoticeDialogListener {
		public void onDialogClick(DialogFragment dialog, int res);
	}
}
